<template>
  <div>
    <!-- <el-dropdown-menu>
      <el-dropdown-item>个人信息</el-dropdown-item>
      <el-dropdown-item>修改密码</el-dropdown-item>
      <el-dropdown-item @click="$router.push('/login')">退出登录</el-dropdown-item>
    </el-dropdown-menu> -->
    <el-row>
      <el-button type="primary" @click="selectAll">查看表单数据</el-button>
      <el-button type="primary" @click="showAddUserDialog"
        >添加用户信息</el-button
      >
    </el-row>

    <el-table :data="navigationData" style="width: 100%">
      <!-- <el-table-column prop="index" label="编号 " width="180">
      </el-table-column> -->
      <el-table-column prop="title" label="文本标题" width="180">
      </el-table-column>
      <el-table-column prop="url" label="图片地址"> </el-table-column>
      <el-table-column prop="date" label="日期" width="180"> </el-table-column>
      <el-table-column label="功能选项区域">
        <template slot-scope="navigationData">
          <el-button
            type="text"
            @click="
              removeNavigationById(navigationData.row, navigationData.$index)
            "
            >删除信息</el-button
          >
          <el-button
            type="text"
            @click="
              updateNavigation(navigationData.row), (dialogFormVisible = true)
            "
            >编辑信息</el-button
          >
        </template>

        <!-- </el-row> -->
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm">
        <el-form-item label="文章标题" :label-width="formLabelWidth">
          <el-input
            v-model="ruleForm.carouselmap.title"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="公众号链接" :label-width="formLabelWidth">
          <el-input
            v-model="ruleForm.carouselmap.url"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="日期" required :label-width="formLabelWidth">
          <el-col :span="11">
            <el-form-item prop="date1">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="ruleForm.carouselmap.date"
                style="width: 100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">保存添加</el-button>
        <el-button type="primary" @click="update">保存修改信息</el-button>
      </div>
    </el-dialog>

    <!-- 分页插件   @= v-on   := v-bind -->
    <div class="block">
      <!-- <span class="demonstration">完整功能</span> -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[1, 5, 10, 15]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formLabelWidth: "120px",
      dialogFormVisible: false,
      navigationData: [], ///存储navigationData的数据
      ruleForm: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
        carouselmap: {
          title: "",
          date: "",
          url: "",
        },
        EditAndAdd: "",
        flag: false,
      },
      rules: {
        title: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        url: [{ required: true, message: "请选择活动区域", trigger: "change" }],
        date: [
          {
            type: "date",
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
      },
      currentPage4: 5,
      total: 20,
      currentPage: 1,
      pageSize:5,
      title: "添加用户信息"

    };
  },
  methods: {
    //1.查看表单数据
    selectAll() {
      this.$carouselmap({
        url: "/api/eventsweb/navigation/findAllNavigation",
        method: "GET",
      }).then((res) => {
        console.log(res.data);
        this.navigationData = res.data;
      });
    },
    //发送删除请求
    removeNavigationById(rowData, index) {
      // alert("removeNavigationById");
      // console.log(rowData.id);
      this.$carouselmap({
        // url: `/api/eventsweb/navigation/removeById/${rowData.id}`,
        url: "/api/eventsweb/navigation/removeById/" + rowData.id,
        method: "DELETE",
      }).then((res) => {
        this.$confirm("是否删除信息, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.$message({
              type: "success",
              message: "恭喜你，删除成功!",
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "取消删除",
            });
          });
        // console.log(res);
        this.handleCurrentChange();
      });
    },
    //展示用户添加界面
    showAddUserDialog(){
      this.ruleForm.carouselmap.title = "";
      this.ruleForm.carouselmap.date = "";
      this.ruleForm.carouselmap.url = "";
      this.dialogFormVisible = true
    },
    //添加信息
    submit() {
      this.title = "添加用户信息"
      //添加前 先清空
 
      this.ruleForm.flag = true;

      // 复制给EditAndAdd
      this.EditAndAdd = "Add添加";

      this.$carouselmap({
        url: "/api/eventsweb/navigation/save",
        method: "POST",
        headers: {
          'Content-Type': 'application/json'
        },
        data: this.ruleForm.carouselmap,
      }).then((res) => {
        this.$confirm("是否添加信息, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.$message({
              type: "success",
              message: "恭喜你，成功添加!",
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消添加",
            });
          });
      });
       this.ruleForm.carouselmap = "";
      this.dialogFormVisible = false;

    },
    //编辑前获取信息
    updateNavigation(rowData) {
       this.title = "修改用户信息"
      this.$carouselmap({
        url: "/api/eventsweb/navigation/info/" + rowData.id,
        method: "GET",
      }).then((res) => {
        console.log(res);
        if (res.status == 200) {
          this.ruleForm.carouselmap = res.data.navigation;
        }
      });
    },
    //修改当前数据
    update() {
      let pageCurt = this.currentPage;      
      this.$carouselmap({
        url: "/api/eventsweb/navigation/update",
        method: "POST",
        data: this.ruleForm.carouselmap,
      }).then((res) => {
        this.dialogFormVisible = false;
        if(res.id!=null || res.id!=0){
          this.$message({
          type: "success",
          message: "恭喜你，修成功!",
        })
          
        }else{
          this.$message({
            type: "info",
            message: "更新失败,请检查网络",
          });
        }
        
        });
        this.handleCurrentChange();

      //刷新页面
      this.currentPage =  pageCurt  
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    //分页查询
    handleCurrentChange(val = 1) {
      this.$carouselmap({
        url: "/api/eventsweb/navigation/findByPage/"+val+"/"+this.pageSize,
        method: "GET",
      }).then((res) => {
        console.log(res);
        this.navigationData = res.data.records;
        this.total = res.data.total
      });
    },
  },
  mounted() {
    // this.selectAll();
    this.handleCurrentChange();
  },
};
</script>